<template>
  <div id="pictureWaterfall">
    <Waterfall
      :lazyload="false"
      :breakpoints="breakpoints"
      :gutter="8"
      :list="pictureList"
      :column-count="3"
      :observe-resize="true"
      :throttle-wait="200"
      :throttle-scroll="true"
    >
      <template #item="{ item: picture }">
        <div class="card_content" @click="handlePictureClick(picture)">
          <div class="card_img">
            <img
              :src="picture.thumbnailUrl ?? picture.url"
              loading="lazy"
              :alt="picture.name"
              class="cover"
            />
            <div class="image-overlay">
              <div class="meta-content">
                <h3 class="title">{{ picture.name }}</h3>
                <div class="tags-container">
                  <a-tag color="green">{{ picture.category ?? '默认' }}</a-tag>
                  <a-tag v-for="tag in picture.tags" :key="tag" class="tag-item">
                    {{ tag }}
                  </a-tag>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </Waterfall>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { Waterfall } from 'vue-waterfall-plugin-next'
import 'vue-waterfall-plugin-next/dist/style.css'

// 定义组件接收的属性
const props = defineProps({
  pictureList: {
    type: Array,
    required: true,
    default: () => [],
  },
  lazyload: {
    type: Boolean,
    default: true,
  },
})

// 定义组件触发的事件
const emit = defineEmits(['pictureClick'])

// 瀑布流布局断点配置
const breakpoints = ref({
  3000: {
    rowPerView: 5, // 一行5图
  },
  1800: {
    rowPerView: 4, // 一行4图
  },
  1200: {
    rowPerView: 3, // 一行3图
  },
  500: {
    rowPerView: 2, // 一行2图
  },
})

// 处理图片点击事件
const handlePictureClick = (picture) => {
  emit('pictureClick', picture)
}
</script>

<style scoped>
.card_content {
  width: 100%;
  margin-bottom: 16px;
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition:
    transform 0.3s ease,
    box-shadow 0.3s ease;
}

.card_content:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card_img {
  position: relative;
  width: 100%;
  padding-bottom: 75%; /* 4:3 宽高比 */
  overflow: hidden;
}

.cover {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.card_content:hover .cover {
  transform: scale(1.05);
}

.image-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  opacity: 0;
  transition: opacity 0.3s ease;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 16px;
}

.card_content:hover .image-overlay {
  opacity: 1;
}

.meta-content {
  transform: translateY(20px);
  transition: transform 0.3s ease;
}

.card_content:hover .meta-content {
  transform: translateY(0);
}

.title {
  color: #fff;
  margin-bottom: 12px;
  font-size: 16px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tag-item {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: none;
  margin: 0;
  padding: 2px 8px;
  font-size: 12px;
}
</style>
